﻿
using System;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using Pantry.Commons;

namespace Pantry.DataAccess
{
    public class TypeOfBusinessSearchDao
    {
        #region Constant

        private const string SqlTypeOfBusinessSearch = @" SELECT Id,SicCode,TypeName FROM TypeOfBusiness";

        private const string SqlTypeOfBusinessById = @"SELECT Id, SicCode, TypeName FROM TypeOfBusiness WHERE Id = @BusinessId";

        #endregion
        #region Menthod

        /// <summary>
        /// Gets the addresses to data set.
        /// </summary>
        /// <param name="connectionString">The connection string.</param>
        /// <returns></returns>
        public static DataSet GetAddressesToDataSet(SqlConnection connectionString)
        {
            var connection = connectionString;
            return SqlHelper.ExecuteDataSet(connection, CommandType.Text, SqlTypeOfBusinessSearch);
        }
        /// <summary>
        /// Returns the search value.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="code">The code.</param>
        /// <param name="connectionString">The connection string.</param>
        /// <returns></returns>
        public DataView ReturnSearchValue(string name, string code, SqlConnection connectionString)
        {
            var dt = GetAddressesToDataSet(connectionString).Tables[0];
            try
            {
                var dataView = dt.DefaultView;
                if (name.Length > 0 && code.Length > 0)
                {
                    dataView.RowFilter = string.Format(CultureInfo.CurrentCulture,
                                                       "TypeName LIKE '%" + name + "%' AND SicCode = '" + code + "'");
                }
                if (name.Length > 0 && code.Length <= 0)
                {
                    dataView.RowFilter = string.Format(CultureInfo.CurrentCulture, "TypeName LIKE '%" + name + "%'");
                }
                if (name.Length <= 0 && code.Length > 0)
                {

                    dataView.RowFilter = string.Format(CultureInfo.CurrentCulture, "SicCode = '" + code + "'");
                }
                if (name.Length <= 0 && code.Length <= 0)
                {
                    return null;
                }
                return dataView;
            }
            catch (SqlException exception)
            {

                ExceptionHandler.WriteErrorLog(exception);
                return null;
            }

        }
        /// <summary>
        /// Types the of business by id.
        /// </summary>
        /// <param name="businessId">The businessid.</param>
        /// <returns></returns>
        public static DataTable TypeOfBusinessById(int businessId)
        {
            try
            {
                var sqlConnection = SqlConnectionTool.SqlGetConnection();
                var parameter = new SqlParameter("@BusinessId", SqlDbType.Int) { Value = businessId };
                return SqlHelper.ExecuteDataSet(sqlConnection, CommandType.Text, SqlTypeOfBusinessById, parameter).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }
        }

        #endregion
    }
}
